package com.dyrj.base.system.apkCtrl.controller;


import com.dyrj.api.result.JsonResult;
import com.dyrj.base.system.apkCtrl.dao.TCtrlAppVersionMapper;
import com.dyrj.base.system.apkCtrl.model.TCtrlAppVersion;
import com.dyrj.base.system.apkCtrl.service.ApkCtrlService;
import com.dyrj.component.OperateLog;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName: ApkController
 * @Description: (APK管理)
 * @author zhaojinbao
 * @date 2019年04月01日 20:52:14
 */

@Controller
@RequestMapping("/system/Apk")
public class ApkController {
@Resource
private ApkCtrlService service;
@Resource
TCtrlAppVersionMapper tCtrlAppVersionMapper;

    @GetMapping("/index")
    @RequiresPermissions("system:Apk:index")
    public String index() {
        return "system/Apk/Apk";
    }

    @GetMapping("/table")
    @ResponseBody
    @RequiresPermissions("system:Apk:index")
    public PageResultMsg table(TCtrlAppVersion record) {
        return service.table(record);
    }

    @GetMapping("/tenant")
    @ResponseBody
    public PageResultMsg tenant(TCtrlAppVersion record) {
        return service.tenant(record);
    }


    @ApiOperation(value="获取版本更新", notes="根据版本号对比获取更新")
    @OperateLog("获取版本更新接口")
    @GetMapping("/checkversion")
    @ResponseBody
    public JsonResult checkversion() {
        JsonResult result = new JsonResult();

        List<TCtrlAppVersion> list = tCtrlAppVersionMapper.selectListVersion();

        if(list!=null && list.size()>0){
            Map<String,Object> map_obj = new HashMap<>();

            map_obj.put("list",list);
            result.setSuccess(true);
            result.setMesssage("获取最新安装包成功！");
            result.setData(map_obj);
        }else{
            result.setSuccess(false);
            result.setMesssage("失败！");
            result.setData("没有查询到新版本信息！");
        }


        return result;
    }

    @PostMapping("/")
    @ResponseBody
    public ResultMsg add(TCtrlAppVersion record) {
        return service.add(record);
    }

    @ResponseBody
    @RequestMapping("/uploadapk")
    public ResultMsg uploadTest(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request){

        String str_classpath = this.getClass().getResource("/").toString();
        System.out.println("str_classpath:"+str_classpath);
        return service.uploadJarFile(file, request);
    }

    @GetMapping("/{id}")
    @ResponseBody
    public TCtrlAppVersion info(@PathVariable Long id) {
        return service.info(id);
    }

    /**
     * @Title: update
     * @Description: (更新定时任务)
     * @param record
     * @return ResultMsg    返回类型
     */
    @PutMapping("/")
    @ResponseBody
    public ResultMsg update(TCtrlAppVersion record) {
        return service.update(record);
    }

    /**
     * @Title: delete
     * @Description: (删除定时任务)
     * @param id
     * @return ResultMsg    返回类型
     */
    @DeleteMapping("/{id}")
    @ResponseBody
    public ResultMsg delete(@PathVariable Long id) {
        return service.delete(id);
    }




}
